package com.kfm.pm.ginshin;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;

public class RoleManage {

    private final ArrayList arrayList = new ArrayList();

    public RoleManage() {
        arrayList.add(new Role("魈", "风", 5, 1, 90));
        arrayList.add(new Role("枫原万叶", "风", 5, 1, 90));
        arrayList.add(new Role("胡桃", "火", 5, 1, 89));
        arrayList.add(new Role("夜兰", "水", 5, 0, 89));
        arrayList.add(new Role("雷电将军", "雷", 5, 0, 89));
        arrayList.add(new Role("纳西妲", "草", 5, 1, 89));
        arrayList.add(new Role("神里绫华", "冰", 5, 0, 90));
        arrayList.add(new Role("行秋", "水", 4, 6, 89));
    }

    public void addRole(Role role) {
        arrayList.add(role);
    }

    public void showAll() {
        System.out.println(arrayList);
//        arrayList.forEach(obj -> System.out.println(obj));
    }

    // 按照等级排序
    public void sortByLevel() {
//        for (int i = 0; i < arrayList.size() - 1; i ++) {
//            for (int j = 0; j < arrayList.size() - 1 -i; j ++) {
////                arrayList.get(j)
////                arrayList.get(j + 1)
//                if ()  {
//
//                }
//            }
//        }
//        arrayList.forEach(obj -> {
//
//        });

        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            iterator.next();
        }
        arrayList.sort(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Role r1 = (Role) o1;
                Role r2 = (Role) o2;
                return r1.getLevel() - r2.getLevel();
            }
        });
    }

    // 按照稀有度
    public void sortByStar() {
        arrayList.sort(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Role r1 = (Role)o1;
                Role r2 = (Role)o2;
                return r1.getStar() -  r2.getStar();
            }
        });
    }

    public void removeLevelLE(int level) {
        // 获取每一个对象，然后获取他的level属性判断是否小于level 小于就删除
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()) {
            Role role = (Role)iterator.next();
            if (role.getLevel() < level) {
                iterator.remove();
            }
        }
    }

    public void sortByName() {
        arrayList.sort(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Role r1 = (Role)o1;
                Role r2 = (Role)o2;
                return r1.getName().compareTo(r2.getName());
            }
        });
    }
}
